package concurrent.util;

import java.util.concurrent.Phaser;

/**
 * User: Administrator
 * Date: 2020/12/1
 * Time: 16:01
 * Description:
 */
public class PhaserDemo2 {

    public static void main(String[] args) {
        Phaser phaser = new Phaser(4) {
            @Override
            protected boolean onAdvance(int phase, int registeredParties) {
                System.out.println("=======phase: " + phase + " finished=============");
                return super.onAdvance(phase, registeredParties);
            }
        };

        for (int i = 0; i < 3; i++) {
            new Thread(()->{
                for (int j = 0; j < 4; j++) {
                    System.out.println(String.format("%s: 阶段: %d", Thread.currentThread().getName(), j));
                    phaser.arriveAndAwaitAdvance();
                }
            }, "Thread " + i).start();
        }

    }
}
